package com.hm.base.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.base.auto.helper.HmSessionFactory;
import com.hm.base.auto.su.ControllerResult;
import com.hm.base.auto.su.R.LogConfig;
import com.hm.base.auto.su.R.Restful;
import com.hm.base.service.MenuService;
import com.hm.base.vo.MenuVo;
import com.hm.common.annotation.WebClientOperationLog;
import com.hm.common.su.bean.ServerResponse;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 下午9:45:08 2017年8月17日
 * @version 1.0
 * @describe
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/menu")
public class MenuApi {

	private static final String SWWAGER_TAG = "菜单信息管理";

	@Autowired
	private MenuService menuService;
	
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增菜单信息", httpMethod = "POST", notes = "com.hm.base.api.MenuApi::addMenu")
	@WebClientOperationLog(platform=LogConfig.BUS_SYSTEM,module=LogConfig.MENU_MODULE,describe="新增菜单信息")
	public ResponseEntity<ServerResponse<Boolean>> addMenu(
			@ApiParam(required = true, name = "body") @RequestBody(required = true) MenuVo menuVo) {
		log.debug("用户{}新增菜单信息", HmSessionFactory.currentUserId());
		menuVo.setCreateUser(HmSessionFactory.currentUserId());
		menuService.addMenu(menuVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/{menuId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取菜单基本信息", httpMethod = "GET", notes = "com.hm.base.api.MenuApi::getMenu")
	public ResponseEntity<ServerResponse<MenuVo>> getMenu(
			@ApiParam(required = true, name = "menuId", value = "菜单ID") @PathVariable("menuId") Long menuId) {
		log.debug("用户{}获取菜单基本信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(menuService.getMenu(menuId));
	}

	@RequestMapping(value = "/{menuId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改菜单基本信息", httpMethod = "PUT", notes = "com.hm.base.api.MenuApi::updateMenu")
	@WebClientOperationLog(platform=LogConfig.BUS_SYSTEM,module=LogConfig.MENU_MODULE,describe="修改[#{#menuId}]菜单信息")
	public ResponseEntity<ServerResponse<Boolean>> updateMenu(
			@ApiParam(required = true, name = "menuId", value = "菜单ID") @PathVariable("menuId") Long menuId,
			@ApiParam(required = true, name = "body") @RequestBody(required = true) MenuVo menuVo) {
		log.debug("用户{}修改菜单基本信息", HmSessionFactory.currentUserId());
		menuVo.setId(menuId);
		menuService.updateMenu(menuVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除菜单基本信息", httpMethod = "DELETE", notes = "com.hm.base.api.MenuApi::deleteMenu")
	@WebClientOperationLog(platform=LogConfig.BUS_SYSTEM,module=LogConfig.MENU_MODULE,describe="删除[#{#menuIds}]菜单信息")
	public ResponseEntity<ServerResponse<Boolean>> deleteMenu(
			@ApiParam(required = true, name = "menuIds", value = "菜单ID") @RequestParam(required = true, name = "menuIds") List<Long> menuIds) {
		log.debug("用户{}删除菜单基本信息", HmSessionFactory.currentUserId());
		menuService.deleteMenu(menuIds);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取系统菜单树数据", httpMethod = "GET", notes = "com.hm.base.api.MenuApi::getSystemMenus")
	public ResponseEntity<ServerResponse<List<MenuVo>>> getSystemMenus() {
		log.debug("用户{},获取系统菜单树数据", HmSessionFactory.currentUserId());
		return ControllerResult.success(menuService.getSystemMenus(0l));
	}

	@RequestMapping(value = "/query/list", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取菜单列表数据", httpMethod = "GET", notes = "com.hm.base.api.MenuApi::queryList")
	public ResponseEntity<ServerResponse<List<MenuVo>>> queryList(
			@ApiParam(required = false, name = "parentId", value = "上级菜单Id") @RequestParam(required = false, name = "parentId") Long parentId) {
		log.debug("用户{},获取菜单列表数据", HmSessionFactory.currentUserId());
		return ControllerResult.success(menuService.queryList(parentId));
	}
}
